/*
 * Copyright [2022] [https://www.xiaonuo.vip]
 *
 * Snowy采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：
 *
 * 1.请不要删除和修改根目录下的LICENSE文件。
 * 2.请不要删除和修改Snowy源码头部的版权声明。
 * 3.本项目代码可免费商业使用，商业使用请保留源码和相关描述文件的项目出处，作者声明等。
 * 4.分发源码时候，请注明软件出处 https://www.xiaonuo.vip
 * 5.不可二次分发开源参与同类竞品，如有想法可联系团队xiaonuobase@qq.com商议合作。
 * 6.若您的项目无法满足以上几点，需要更多功能代码，获取Snowy商业授权许可，请在官网购买授权，地址为 https://www.xiaonuo.vip
 */
package vip.xiaonuo.biz.modular.wall_category.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollStreamUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.solon.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.noear.solon.annotation.Component;
import org.noear.solon.data.annotation.Cache;
import org.noear.solon.data.annotation.CacheRemove;
import org.noear.solon.data.annotation.Tran;
import vip.xiaonuo.biz.core.enums.BizDataTypeEnum;
import vip.xiaonuo.biz.model.common.SelectResult;
import vip.xiaonuo.biz.modular.wall_category.entity.BizWallCategory;
import vip.xiaonuo.biz.modular.wall_category.mapper.BizWallCategoryMapper;
import vip.xiaonuo.biz.modular.wall_category.param.BizWallCategoryAddParam;
import vip.xiaonuo.biz.modular.wall_category.param.BizWallCategoryEditParam;
import vip.xiaonuo.biz.modular.wall_category.param.BizWallCategoryIdParam;
import vip.xiaonuo.biz.modular.wall_category.param.BizWallCategoryPageParam;
import vip.xiaonuo.biz.modular.wall_category.service.BizWallCategoryService;
import vip.xiaonuo.common.enums.CommonSortOrderEnum;
import vip.xiaonuo.common.exception.CommonException;
import vip.xiaonuo.common.listener.CommonDataChangeEventCenter;
import vip.xiaonuo.common.page.CommonPageRequest;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 壁纸分类Service接口实现类
 *
 * @author KingTiger
 * @date 2025/05/27 16:08
 **/
@Slf4j
@Component
public class BizWallCategoryServiceImpl extends ServiceImpl<BizWallCategoryMapper, BizWallCategory> implements BizWallCategoryService {

    @Override
    public Page<BizWallCategory> page(BizWallCategoryPageParam bizWallCategoryPageParam) {
        QueryWrapper queryWrapper = new QueryWrapper();
        if (ObjectUtil.isNotEmpty(bizWallCategoryPageParam.getName())) {
            queryWrapper.like(BizWallCategory::getName, bizWallCategoryPageParam.getName());
        }
        if (ObjectUtil.isAllNotEmpty(bizWallCategoryPageParam.getSortField(), bizWallCategoryPageParam.getSortOrder())) {
            CommonSortOrderEnum.validate(bizWallCategoryPageParam.getSortOrder());
            queryWrapper.orderBy(StrUtil.toUnderlineCase(bizWallCategoryPageParam.getSortField()), bizWallCategoryPageParam.getSortOrder().equals(CommonSortOrderEnum.ASC.getValue()));
        } else {
            queryWrapper.orderBy(BizWallCategory::getSortCode).asc();
        }
        return this.page(CommonPageRequest.defaultPage(), queryWrapper);
    }

    @Tran
    @Override
    @CacheRemove(tags = "WallCache")
    public void add(BizWallCategoryAddParam bizWallCategoryAddParam) {
        BizWallCategory bizWallCategory = BeanUtil.toBean(bizWallCategoryAddParam, BizWallCategory.class);
        this.save(bizWallCategory);
        CommonDataChangeEventCenter.doAddWithData(BizDataTypeEnum.WallCategory.getValue(), JSONUtil.createArray().put(bizWallCategory));
    }

    @Tran
    @Override
    @CacheRemove(tags = "WallCache")
    public void edit(BizWallCategoryEditParam bizWallCategoryEditParam) {
        BizWallCategory bizWallCategory = this.queryEntity(bizWallCategoryEditParam.getId());
        BeanUtil.copyProperties(bizWallCategoryEditParam, bizWallCategory);
        this.updateById(bizWallCategory);
        CommonDataChangeEventCenter.doUpdateWithData(BizDataTypeEnum.WallCategory.getValue(), JSONUtil.createArray().put(bizWallCategory));
    }

    @Tran
    @Override
    @CacheRemove(tags = "WallCache")
    public void delete(List<BizWallCategoryIdParam> bizWallCategoryIdParamList) {
        // 执行删除
        List<String> idList = CollStreamUtil.toList(bizWallCategoryIdParamList, BizWallCategoryIdParam::getId);
        this.removeByIds(idList);
        CommonDataChangeEventCenter.doDeleteWithDataId(BizDataTypeEnum.WallCategory.getValue(), idList);
    }

    @Override
    public BizWallCategory detail(BizWallCategoryIdParam bizWallCategoryIdParam) {
        return this.queryEntity(bizWallCategoryIdParam.getId());
    }

    @Override
    public BizWallCategory queryEntity(String id) {
        BizWallCategory bizWallCategory = this.getById(id);
        if (ObjectUtil.isEmpty(bizWallCategory)) {
            throw new CommonException("壁纸分类不存在，id值为：{}", id);
        }
        return bizWallCategory;
    }

    @Override
    public List<SelectResult> getAllSelList() {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.orderBy(BizWallCategory::getSortCode).asc();
        List<BizWallCategory> barMenuList = this.list(queryWrapper);
        List<SelectResult> list = barMenuList.stream().map(item -> new SelectResult(item.getId(), item.getName())).collect(Collectors.toList());
        return list;
    }

    @Cache(key = "WallCache::Wall::Category::getNameById::${id}", service = "CacheRedis", tags = "WallCache", seconds = 86400)
    @Override
    public String getNameById(String id) {
        QueryWrapper queryWrapper = QueryWrapper.create();
        queryWrapper.select(BizWallCategory::getName);
        queryWrapper.eq(BizWallCategory::getId, id);
        BizWallCategory bizWallCategory = mapper.selectOneByQuery(queryWrapper);
        if (ObjectUtil.isNull(bizWallCategory)) {
            return null;
        }
        String name = bizWallCategory.getName();
        log.info("id={},name={}", id, name);
        return name;
    }
}
